package com.xxx.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.demo.dto.ShowAllDto;
import com.xxx.demo.entity.ShowType;
import com.xxx.demo.service.ShowTypeService;
import com.xxx.demo.mapper.ShowTypeMapper;
import com.xxx.demo.vo.ShowAllVo;
import com.xxx.demo.vo.ShowTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

/**
 *
 */
@Service
public class ShowTypeServiceImpl extends ServiceImpl<ShowTypeMapper, ShowType>
    implements ShowTypeService {
    @Autowired(required = false)
    private ShowTypeMapper showTypeMapper;

    /**
     *
     * 查询所有分类(用于下拉框)
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<ShowTypeVo> selectAll(){
        List<ShowTypeVo> showTypeVos = showTypeMapper.selectAll();
        if (CollectionUtil.isNotEmpty(showTypeVos)){
            for (ShowTypeVo showTypeVo : showTypeVos){
                showTypeVo.setStrId(showTypeVo.getId() + "!");
            }
        }
        return showTypeVos;
    }

    /**
     * 添加分类
     * @param showType
     * @return
     */
    @Override
    @Transactional
    public int addShowType(ShowType showType){
        Random random=new Random();
        showType.setShowType("ST"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss-"))+random.nextInt(100));
        return showTypeMapper.insert(showType);
    }

    /**
     * 分页查询所有展示类型（管理）
     * @param pageNo
     * @param pageSize
     * @param showType
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public IPage<ShowTypeVo> findShowTypePage(Integer pageNo, Integer pageSize, ShowType showType){
        Page<ShowType> page = new Page<>(pageNo,pageSize);
        IPage<ShowType> showTypePage = showTypeMapper.findShowTypePage(page, showType);
        IPage<ShowTypeVo> showTypeVoIPage = new Page<>();
        List<ShowType> records = showTypePage.getRecords();
        List<ShowTypeVo> showTypeVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(records)){
            for(ShowType sh:records){
                ShowTypeVo showTypeVo = new ShowTypeVo();
                BeanUtil.copyProperties(sh,showTypeVo);
                showTypeVo.setStrId(sh.getId() + "!");
                showTypeVos.add(showTypeVo);
            }
        }
        BeanUtil.copyProperties(showTypePage,showTypeVoIPage);
        showTypeVoIPage.setRecords(showTypeVos);
        return showTypeVoIPage;
    }

    /**
     * 根据id查详情
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public ShowType findShowTypeById(Long id){
        return showTypeMapper.selectOne(new QueryWrapper<ShowType>().eq("id", id).eq("delete_flag", 0));
    }

    /**
     * 修改展示类型
     * @param showType
     * @return
     */
    @Override
    @Transactional
    public int updateShowType(ShowType showType){
        ShowType selectOne = showTypeMapper.selectOne(new QueryWrapper<ShowType>().eq("id", showType.getId()).eq("delete_flag", 0));
        if (ObjectUtil.isNotEmpty(selectOne)){
            BeanUtil.copyProperties(showType,selectOne);
            int i = showTypeMapper.updateById(selectOne);
            return i;
        }
        return 0;

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int deleteById(Long id){
        ShowType selectOne = showTypeMapper.selectOne(new QueryWrapper<ShowType>().eq("id", id).eq("delete_flag", 0));
        if (ObjectUtil.isNotEmpty(selectOne)){
           selectOne.setDeleteFlag(1);
            int i = showTypeMapper.updateById(selectOne);
            return i;
        }
        return 0;
    }
}




